﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Autorent.Service.Tests.AutorentMngrServiceReference;

namespace Autorent.Service.Tests.MngrServiceTests
{
    [TestFixture]
    public class AutoClassesCRUD
    {
        [Test]
        public void AutoClassesCreateTest()
        {
            var ctx = AutoRentEntitiesWithToken.GetValidCtx();
            AutoClass newAutoClass = AutoClass.CreateAutoClass(0, "NewAutoClass");
            ctx.AddToAutoClasses(newAutoClass);
            ctx.SaveChanges();
            Assert.NotNull(AutoRentEntitiesWithToken.GetValidCtx().AutoClasses.ToList().Find(c => c.AutoClassName == newAutoClass.AutoClassName));
        }

        [Test]
        public void AutoClassesRetrieveTest() {
            AutoRentEntitiesWithToken.GetValidCtx().AutoClasses.ToList(); // Confirm there is no exception when materializing AutoClasses entity
        }

        [Test]
        public void AutoClassesUpdateTest()
        {
            var ctx = AutoRentEntitiesWithToken.GetValidCtx();
            AutoClass newAutoClass = AutoClass.CreateAutoClass(0, "UpdateAutoClass");
            ctx.AddToAutoClasses(newAutoClass);
            ctx.SaveChanges();
            ctx = AutoRentEntitiesWithToken.GetValidCtx();
            AutoClass newAutoClassFromDB = ctx.AutoClasses.ToList().Find(c => c.AutoClassName == newAutoClass.AutoClassName);
            newAutoClassFromDB.AutoClassInfo = "Updated info";
            ctx.UpdateObject(newAutoClassFromDB);
            ctx.SaveChanges();
            AutoClass updFrDB = AutoRentEntitiesWithToken.GetValidCtx().AutoClasses.ToList().Find(c => c.AutoClassName == newAutoClass.AutoClassName);
            Assert.NotNull(updFrDB);
            Assert.AreEqual(newAutoClassFromDB.AutoClassInfo, updFrDB.AutoClassInfo);
        }

        [Test]
        public void AutoClassesDeleteTest()
        {
            var ctx = AutoRentEntitiesWithToken.GetValidCtx();
            AutoClass newAutoClass = AutoClass.CreateAutoClass(0, "DeleteAutoClass");
            ctx.AddToAutoClasses(newAutoClass);
            ctx.SaveChanges();
            ctx = AutoRentEntitiesWithToken.GetValidCtx();
            AutoClass newAutoClassFromDB = ctx.AutoClasses.ToList().Find(c => c.AutoClassName == newAutoClass.AutoClassName);
            Assert.NotNull(newAutoClassFromDB);
            ctx.DeleteObject(newAutoClassFromDB);
            ctx.SaveChanges();
            Assert.IsNull(AutoRentEntitiesWithToken.GetValidCtx().AutoClasses.ToList().Find(c => c.AutoClassName == newAutoClass.AutoClassName));
        }
    }
}
